<script lang="ts" setup>
import type { UploadFile, UploadFiles, UploadInstance } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

defineOptions({
  name: 'Open',
})
definePageMeta({
  title: '打开', // 自定义元信息
  requiresAuth: false, // 其他自定义元信息
  layout: 'default', // 指定布局
  keepalive: true,
})

const uploadRef = ref<UploadInstance>()

const router = useRouter() // 创建 router 实例

async function handleFileUpload(uploadFile: UploadFile, _uploadFiles: UploadFiles) {
  if (uploadFile.raw) {
    const url = URL.createObjectURL(uploadFile.raw)
    router.push({
      path: '/view',
      query: { url, title: uploadFile.name },
    })
    uploadRef.value?.clearFiles()
  }
}
</script>

<template>
  <div class="box">
    <el-upload ref="uploadRef" class="avatar-uploader" :show-file-list="false" accept=".pdf" @change="handleFileUpload">
      <el-icon class="avatar-uploader-icon">
        <Plus />PDF
      </el-icon>
    </el-upload>
  </div>
</template>

<style scoped>
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  /* 添加此行以实现垂直居中 */
  height: calc(100vh - 300px);
  /* 可选 */
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
